<template>
    <div class="login-container">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form"  auto-complete="on">
        <div class="title-container">
          <div class="title">多流域坝群健康在线监控和综合诊断平台</div>
          <div class="title2">MULTI BASIN DAM GROUP HEALTH ONLINE MONITORING AND COMPREHENSIVE DIAGNOSIS PLATFORM</div>
        </div>
        <div class="login-form-wrapper">
            <img class="login-form-img" :src="require('@/assets/image/login-form.jpg')" alt="">
            <div class="login-form-item">
                <el-form-item prop="username">
                    <el-input
                        placeholder="请输入用户名"
                        prefix-icon="el-icon-user"
                        auto-complete="on"
                        v-model="loginForm.username">
                    </el-input>
                </el-form-item>
        
                <el-form-item prop="password">
                    <el-input placeholder="请输入密码" auto-complete="on" v-model="loginForm.password" show-password prefix-icon="el-icon-lock" keyup.enter.native="handleLogin"></el-input>
                </el-form-item>
        
                <el-button
                    id="login_btn"
                    :loading="loading"
                    type="primary"
                    @click.native.prevent="handleLogin"
                    style="width:320px"
                >登录</el-button>
             </div>
        </div>
      </el-form>
    </div>
  </template>
  
  <script>
  import {GetLogin,getModuleByUser,UserDJTlogin} from "@/api/https";
  export default {
    name: "login",
    data() {
      const validateUsername = (rule, value, callback) => {
        if (value.length < 3) {
          callback(new Error("请输入正确的用户名"));
        } else {
          callback();
        }
      };
      const validatePassword = (rule, value, callback) => {
        if (value.length < 6) {
          callback(new Error("密码不能小于6位数字"));
        } else {
          callback();
        }
      };
      return {
        rebPassword: false,
        rebUsername: false,
        loginForm: {
          username: "",
          password: ""
        },
        loginRules: {
          username: [
            { required: true, trigger: "blur", validator: validateUsername }
          ],
          password: [
            { required: true, trigger: "blur", validator: validatePassword }
          ]
        },
        loading: false,
        loadingDJT:null
      };
    },
    created(){
      if(this.$route.query.isDJT){
        this.loadingDJT = this.$loading({
          lock: true,
          text: '登录验证中，请稍后',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        this.getUserDJTlogin(this.$route.query.ticket)
      }
    },
    methods: {
      getUserDJTlogin(ticket){
        UserDJTlogin({
          tickt:ticket
        }).then(res=>{
         if (res.isSuccess && res.data?.user) {
            this.handleLoginSuccess(res.data.user)
            sessionStorage.setItem("stationId", 3010202)
            sessionStorage.setItem("isLogin", true);
            if(res.data.user.token){
              this.loadingDJT.close();
              this.getAuth()
            }
          }else{
            this.loadingDJT.close();
            this.$message.error(res.errorMessage)
          }
        })
      },
      getAuth(){
        getModuleByUser().then(res=>{
          if(res.isSuccess && res.data.length > 0){
            const authiortyMenu = res.data.map(i =>{
              return i.moduleId     
            })
            sessionStorage.setItem("isLogin", true);
            sessionStorage.setItem("authiortyMenu", authiortyMenu);
            this.$store.commit('changeAuthority',res.data)
            this.loading = false;
            this.$router.push({ path: "/" });
            this.$message.success('登录成功');
          }else{
            this.loading = false;
            this.$message.error('权限获取失败或没有任何权限!');
          }
        })
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (!valid) {
            this.loading = false;
            return;
          }
          
          this.loading = true;
          GetLogin(this.loginForm).then(res => {
            if (res.isSuccess && res.data?.user) {
              this.handleLoginSuccess(res.data.user);
            } else {
              this.$message.error(res.errorMessage || '登录失败');
            }
          }).catch(error => {
            this.$message.error('网络错误，请稍后重试');
          }).finally(() => {
            this.loading = false;
          });
        });
      },
      handleLoginSuccess(user) {
        sessionStorage.clear();
        sessionStorage.setItem("token", user.token); 
        sessionStorage.setItem("userName", user.userName);
        if (user.token) this.getAuth();
      }
    }
  };
  </script>
  

<style lang="scss" scoped>
  $bg: #2d3a4b;
  $dark_gray: #889aa4;
  $light_gray: #eee;
  
  
.login-container {
    height: 100vh;
    width: 100%;
    color: #000;
    background-image: url('../assets/image/login-bg2.png');
    background-size: 100% 100%;
    background-repeat: no-repeat;
    overflow: hidden;
    position: relative;
    .login-form {
      position: absolute;
      top: 22%;
      left:50%;
      width: 800px;
      margin-left:-400px;
    }
    .title-container {
      margin-bottom: 80px;
      color: #d7f4ff;
      .title {
        letter-spacing: 5px;
        color: #d7f4ff;
        font-size: 32px;
        margin: 0 auto;
        text-align: center;
        font-weight: bold;
      }
      .title2 {
        color: #d7f4ff;
        font-size: 13px;
        font-weight: 200;
        margin: 0 auto;
        text-align: center;
        font-weight: bold;
      }
    }
    .login-form-wrapper{
        width:800px;
        height: 280px;
        display: flex;
        flex-direction: row;
        justify-content: start;
        .login-form-img {
            width:400px;
            height: 280px;
        }
        .login-form-item {
            width: 400px;
            height: 280px;
            background: #fff;
            padding:0 40px;
            box-sizing: border-box;
            padding-top:40px;
            ::v-deep .el-input__inner{
                height: 50px;
                line-height: 50px;
            }
        }
    }
  }
  </style>
  